{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is: 60\n",
      "change local x to 3\n",
      "value of x is 3\n"
     ]
    }
   ],
   "source": [
    "x = 60\n",
    "\n",
    "def foo():\n",
    "    global x\n",
    "    print(\"x is: \" + str(x))\n",
    "    x = 3\n",
    "    print(\"change local x to \" + str(x))\n",
    "\n",
    "foo()\n",
    "print('value of x is' , str(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Happy Birthday! \n",
      "Happy Birthday! Happy Birthday! Happy Birthday! Happy Birthday! \n"
     ]
    }
   ],
   "source": [
    "def repeat_str(s, times=1):\n",
    "    repeated_strs = s * times\n",
    "    return repeated_strs\n",
    "\n",
    "repeated_strings = repeat_str(\"Happy Birthday! \")\n",
    "print(repeated_strings)\n",
    "\n",
    "repeated_strings_2 = repeat_str(\"Happy Birthday! \" , 4)\n",
    "print(repeated_strings_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关键字参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: 13 , b: 17 , c: 80\n",
      "a: 12 , b: 40 , c: 24\n",
      "a: 80 , b: 40 , c: 40\n"
     ]
    }
   ],
   "source": [
    "def func(a, b=40, c=80):\n",
    "    print('a:', a, ', b:', b, ', c:', c)\n",
    "\n",
    "func(13, 17)\n",
    "func(12, c=24) # 指定给哪个关键字赋值\n",
    "func(c=40, a=80) # 指定给 a 赋值，可以把它放在后面"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 可变长参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- `*args` 可变长度元组参数\n",
    "- `**kwargs` 可变长度字典参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fpara: hello1\n",
      "args: ()\n",
      "kwargs: {}\n",
      "\n",
      "fpara: hello2\n",
      "args: (1, 3, 5)\n",
      "kwargs: {}\n",
      "\n",
      "fpara: hello3\n",
      "args: ()\n",
      "kwargs: {'ele': 'world3'}\n",
      "\n",
      "fpara: hello4\n",
      "args: (1, 3)\n",
      "kwargs: {'ele': 'world4'}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def var_paras(fpara, *args, **kwargs):\n",
    "    print(\"fpara: \" + str(fpara))\n",
    "    print(\"args: \" + str(args))\n",
    "    print(\"kwargs: \" + str(kwargs))\n",
    "    print()\n",
    "\n",
    "var_paras(\"hello1\")\n",
    "var_paras(\"hello2\", 1, 3, 5)\n",
    "var_paras(\"hello3\", ele=\"world3\")\n",
    "var_paras(\"hello4\", 1, 3, ele=\"world4\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
